package fu.capstone.hanoicityguide.presentation.fragment.tourprovider;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SearchView;
import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.bo.tourprovider.TourProviderBO;
import fu.capstone.hanoicityguide.business.bo.tourprovider.TourProviderBOImp;
import fu.capstone.hanoicityguide.business.model.TourProvider;
import fu.capstone.hanoicityguide.presentation.adapter.ListTourProviderAdapter;

@SuppressLint("ValidFragment")
public class ListTourProviderFragment extends Fragment implements
		SearchView.OnQueryTextListener, SearchView.OnCloseListener {

	private TourProviderBO tourProviderBO;
	private ArrayList<TourProvider> arrTourProvider;
	private String sorter;
	private ListTourProviderAdapter tourProviderAdapter = null;
	private ListView listTourProvider = null;
	private SearchView searchView;
	private String keyword;
	private DrawerLayout drawer;
	private String[] sortArr;

	public ListTourProviderFragment(DrawerLayout drawer) {
		this.drawer = drawer;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// init
		try {
			setHasOptionsMenu(true);
		} catch (Exception e) {
			Log.e("ListTourProviderFragment : ", e.toString());
		}
		View listTourProviderFragment = inflater.inflate(
				R.layout.fragment_list_tourprovider, container, false);
		tourProviderBO = new TourProviderBOImp(container.getContext());
		listTourProvider = (ListView) listTourProviderFragment
				.findViewById(R.id.listTourProvider);

		// set keyword and sorter
		setKeyword("");
		// setFilter("All");
		setSorter(getResources().getStringArray(R.array.sorter_items)[0]);
		sortArr = new String[2];
		sortArr[0] = getResources().getStringArray(R.array.sorter_items)[2];
		sortArr[1] = getResources().getStringArray(R.array.sorter_items)[3];

		// get all list TourProvider
		arrTourProvider = new ArrayList<TourProvider>();
		arrTourProvider = tourProviderBO.listTourProvider(getSorter());

		tourProviderAdapter = new ListTourProviderAdapter(getActivity(),
				R.layout.adapter_list_tourprovider, arrTourProvider);

		// set listview adapter and create event for items click
		listTourProvider.setAdapter(tourProviderAdapter);

		listTourProvider.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				// call DetailTourProviderFragment
				Fragment browseTourProvider = new DetailTourProviderFragment(
						arrTourProvider.get(position).getId(), arrTourProvider
								.get(position).getName());
				if (browseTourProvider != null) {
					FragmentManager fragmentManager = getFragmentManager();
					FragmentTransaction fTransaction = fragmentManager
							.beginTransaction();
					fTransaction.addToBackStack(null);
					fTransaction.replace(R.id.frame_container,
							browseTourProvider).commit();
					getActivity().setTitle(
							arrTourProvider.get(position).getName());
				}
			}
		});
		return listTourProviderFragment;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

		// create option menu in actionbar
		inflater = getActivity().getMenuInflater();
		inflater.inflate(R.menu.list_tourprovider_menu, menu);

		// Associate searchable configuration with the SearchView
		searchView = (SearchView) menu
				.findItem(R.id.action_search_tourprovider).getActionView();
		setupSearchView();
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// create event for items of option menu in actionbar
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		AlertDialog dialog;

		switch (item.getItemId()) {
		// show sorter dialog
		case R.id.action_sort_tourprovider:
			builder.setTitle("Sort");
			builder.setItems(sortArr, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int item) {
					// sort by sorter
					setSorter(getResources().getStringArray(
							R.array.sorter_items)[item + 2]);
					if (getKeyword().equals("")) {
						arrTourProvider = tourProviderBO
								.listTourProvider(getSorter());
					} else {
						arrTourProvider = tourProviderBO.searchTourProvider(
								getKeyword(), getSorter());
					}

					// reset array hotel in adapter
					tourProviderAdapter.setArr(arrTourProvider);
					tourProviderAdapter.notifyDataSetChanged();
				}
			});

			dialog = builder.create();
			dialog.show();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onResume() {
		// on resume change the title of actionbar to TourProvider
		super.onResume();
		getActivity().getActionBar().setTitle("Tour Provider");
	}

	public String getSorter() {
		return this.sorter;
	}

	public void setSorter(String sorter) {
		this.sorter = sorter;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public String getKeyword() {
		return this.keyword;
	}

	private void setupSearchView() {

		searchView.setIconifiedByDefault(false);
		searchView.setQueryHint("Tour Provider Name");

		searchView
				.setOnQueryTextFocusChangeListener(new OnFocusChangeListener() {

					@Override
					public void onFocusChange(View v, boolean hasFocus) {
						// TODO Auto-generated method stub
						if (hasFocus) {
							drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
						} else {
							onClose();
						}
					}
				});

		searchView.setOnQueryTextListener(this);
		searchView.setOnCloseListener(this);
	}

	@Override
	public boolean onQueryTextSubmit(String query) {
		setKeyword(query);
		if (getKeyword() != null && getSorter() != null) {
			arrTourProvider = tourProviderBO.searchTourProvider(getKeyword(),
					getSorter());
			tourProviderAdapter.setArr(arrTourProvider);
			tourProviderAdapter.notifyDataSetChanged();
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean onQueryTextChange(String newText) {
		// TODO Auto-generated method stub
		setKeyword(newText);

		if (getKeyword() != null && getSorter() != null) {
			arrTourProvider = tourProviderBO.searchTourProvider(getKeyword(),
					getSorter());
			tourProviderAdapter.setArr(arrTourProvider);
			tourProviderAdapter.notifyDataSetChanged();
			return true;
		} else {
			return false;
		}

	}

	@Override
	public boolean onClose() {
		// onclose search view
		drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
		searchView.setQuery("", true);
		setKeyword("");
		arrTourProvider = tourProviderBO.listTourProvider(getSorter());
		tourProviderAdapter.setArr(arrTourProvider);
		tourProviderAdapter.notifyDataSetChanged();
		return true;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		searchView = null;
		super.onDestroy();
	}

}
